﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Reflection;
using SouslikPod.Configuration;

namespace SouslikPod.CommandManager
{
    public class CommandLoader
    {
        private const string PLUGIN_DIRECTORY = @"Commands";
        public List<BaseCommand> LoadCommands()
        {
            string[] assemblyNames = Directory.GetFiles(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, PLUGIN_DIRECTORY),"*.dll");
            
            List<BaseCommand> commandsList = new List<BaseCommand>();
            foreach (string assemblyName in assemblyNames)
            {
                Assembly assembly = Assembly.LoadFile(assemblyName);
                Type[] commands = assembly.GetTypes();
                foreach (Type commandType in commands)
                {
                    if (commandType.BaseType != null &&  commandType.IsSubclassOf(typeof(BaseCommand)))
                    {
                        commandsList.Add((BaseCommand)assembly.CreateInstance(commandType.FullName));
                    }
                } 
            }

            return commandsList;
        }
    }
}
